package com.auicyh.tylb.dialog;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auicyh.tylb.R;
import com.auicyh.tylb.adapter.BaseCustomAdapter;
import com.auicyh.tylb.adapter.ViewHolder;
import com.auicyh.tylb.inter.DialogItemCallBacks;
import com.auicyh.tylb.model.ImageTextModel;
import com.auicyh.tylb.utils.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 作者：  Ocean 2018/8/28 15:34
 * 信息：  Oil com.auicyh.tylb.dialog
 * 功能：  权限提示对话框，使用GridView，也可传入数据展示其它内容。默认显示三列
 * 邮箱：   1126067113@qq.com
 * 修改人：
 * 公司: 北京天元陆兵汽车科技有限公司
 * @author Ocean
 */
public class DialogPerHintFragment extends BaseDialogFragment implements AdapterView.OnItemClickListener {
    /** GridView列数 */
    private int mColumns = 0;
    /** 是否显示确定按钮(默认显示) */
    private boolean isPositive = true;
    /** 一个数据集合 */
    List<ImageTextModel> mImageTextModels;
    private DialogItemCallBacks mItemCallBacks;

    /** 数据集合 */
    private static final String KEY_IMAGE_TEXT_DATA = "key_image_text_data";

    /**
     * @param title     标题栏
     * @param message   提示信息
     * @param modelList 数据集合
     * @param positive  积极的
     * @return DialogPerHintFragment
     */
    public static DialogPerHintFragment newInstance(String title, String message, List<ImageTextModel> modelList, String positive) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_DIALOG_TITLE, title);
        bundle.putString(KEY_DIALOG_MESSAGE, message);
        bundle.putParcelableArrayList(KEY_IMAGE_TEXT_DATA, (ArrayList<? extends Parcelable>) modelList);
        bundle.putString(KEY_DIALOG_POSITIVE, positive);
        DialogPerHintFragment hintFragment = new DialogPerHintFragment();
        hintFragment.setArguments(bundle);
        return hintFragment;
    }

    public static DialogPerHintFragment newInstance(String title, List<ImageTextModel> modelList) {
        return newInstance(title, "", modelList, "");
    }

    public static DialogPerHintFragment newInstance(Activity activity, int title, List<ImageTextModel> modelList, int positive) {
        return newInstance(activity.getString(title), activity.getString(title), modelList, activity.getString(positive));
    }

    public static DialogPerHintFragment newInstance(Activity activity, int title, int positive) {
        ImageTextModel[] mDataModes = {
                new ImageTextModel(R.string.tylb_dialog_phone, R.mipmap.tylb_per_phone),
                new ImageTextModel(R.string.tylb_dialog_folder, R.mipmap.tylb_per_folder),
                new ImageTextModel(R.string.tylb_dialog_location, R.mipmap.tylb_per_location),
                new ImageTextModel(R.string.tylb_dialog_voice, R.mipmap.tylb_per_voice),
                new ImageTextModel(R.string.tylb_dialog_bluetooth, R.mipmap.tylb_per_bluetooth),
        };
        List<ImageTextModel> models = new ArrayList<>();
        Collections.addAll(models, mDataModes);
        return newInstance(activity.getString(title), "", models, activity.getString(positive));
    }


    public DialogPerHintFragment() {
        setHighContent(true);
        setShowClose(false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            setTitle(bundle.getString(KEY_DIALOG_TITLE));
            setMessage(bundle.getString(KEY_DIALOG_MESSAGE));
            setPositive(bundle.getString(KEY_DIALOG_POSITIVE));
            mImageTextModels = bundle.getParcelableArrayList(KEY_IMAGE_TEXT_DATA);
        }
    }

    @Override
    public void onViewDialog(FrameLayout frameLayout) {
        View view = View.inflate(getActivity(), R.layout.dialog_grid_view, null);
        frameLayout.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        GridView gridView = view.findViewById(R.id.tylb_dialog_grid_view);
        TextView hintMessage = view.findViewById(R.id.tylb_dialog_grid_view_message);
        hintMessage.setVisibility(View.VISIBLE);
        if (!StringUtils.isStrNull(getMessage())) {
            hintMessage.setText(getMessage());
        }
        if (mColumns != 0) {
            gridView.setNumColumns(mColumns);
        }
        gridView.setAdapter(new BaseCustomAdapter<ImageTextModel>(getActivity(), mImageTextModels, R.layout.item_image_text) {
            @Override
            protected void onConvert(ViewHolder viewHolder, View convertView, ImageTextModel itemData, int position) {
                Activity activity = getActivity();
                if (activity != null) {
                    Drawable drawable = ContextCompat.getDrawable(getActivity(), itemData.getImageRes());
                    String txt = getString(itemData.getNameRes());
                    viewHolder.setTopImageText(convertView, R.id.tylb_dialog_single_tv, txt, drawable);
                }
            }
        });
        gridView.setOnItemClickListener(this);
    }

    @Override
    public void onChildView(LinearLayout linearLayout, TextView titleTv, ImageButton closeIb, Button n, Button p) {
        // linearLayout.setVisibility(View.GONE);
        if (isPositive) {
            p.setVisibility(View.VISIBLE);
        } else {
            p.setVisibility(View.GONE);
        }
    }

    public int getColumns() {
        return mColumns;
    }

    public void setColumns(int columns) {
        mColumns = columns;
    }

    public boolean isPositive() {
        return isPositive;
    }

    public void setPositive(boolean positive) {
        isPositive = positive;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mItemCallBacks != null) {
            mItemCallBacks.onItemClick(mImageTextModels.get(position), position);
        }
    }

    public DialogItemCallBacks getItemCallBacks() {
        return mItemCallBacks;
    }

    public void setItemCallBacks(DialogItemCallBacks itemCallBacks) {
        mItemCallBacks = itemCallBacks;
    }
}
